package com.bndg.littletor.ui.home

import android.content.Intent
import android.nfc.NfcAdapter
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.bndg.littletor.databinding.FragmentHomeBinding

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

        homeViewModel.text.observe(viewLifecycleOwner) {
        }
        binding.btnNfcScan.setOnClickListener {
            Log.e(">>>>", "1112225544")
            val intent = Intent(requireContext(), NfcReaderActivity::class.java)
            startActivity(intent)
        }
        binding.btnBarcodeScan.setOnClickListener {
            val intent = Intent(requireContext(), BarcodeScanActivity::class.java)
            startActivity(intent)
        }

        return root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val nfcAdapter = NfcAdapter.getDefaultAdapter(requireContext());
        if (nfcAdapter == null) {
            // 设备不支持 NFC
            Toast.makeText(requireContext(), "该设备不支持 NFC", Toast.LENGTH_SHORT).show();
        } else if (!nfcAdapter.isEnabled()) {
            // 提示用户开启 NFC
            Toast.makeText(requireContext(), "请开启 NFC 功能", Toast.LENGTH_SHORT).show();
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}